package com.bci.pwtz.service.impl;

import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.CompanyUserAuditMapper;
import com.bci.pwtz.mysql.dao.UserCompanyRefMapper;
import com.bci.pwtz.mysql.model.UserCompanyRef;
import com.bci.pwtz.service.UserCompanyRefService;
@Service
public class UserCompanyRefServiceImpl implements UserCompanyRefService
{
    private UserCompanyRefMapper userCompanyRefMapper;
    private CompanyUserAuditMapper companyUserAuditMapper;
    private static final Logger logger=Logger.getLogger(UserCompanyRefServiceImpl.class);
    
    public UserCompanyRefMapper getUserCompanyRefMapper()
    {
        return userCompanyRefMapper;
    }

    public void setUserCompanyRefMapper(UserCompanyRefMapper userCompanyRefMapper)
    {
        this.userCompanyRefMapper = userCompanyRefMapper;
    }

    public CompanyUserAuditMapper getCompanyUserAuditMapper()
    {
        return companyUserAuditMapper;
    }

    public void setCompanyUserAuditMapper(CompanyUserAuditMapper companyUserAuditMapper)
    {
        this.companyUserAuditMapper = companyUserAuditMapper;
    }

    @Override
    public List<UserCompanyRef> findUserCompanyRefList(Long userId) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("findUserCompanyRefList: userId="+userId);
        }
        if(userId==null){
            if(logger.isDebugEnabled()){
                logger.debug("findUserCompanyRefList,userId is null");
            }
            return null;
        }
        List<UserCompanyRef> list=null;
        try{
            list=userCompanyRefMapper.selectUserCompanyRefList(userId);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
         if (list!=null)
        {
            logger.debug("findUserCompanyRefList list=" + list.toString());
        }
        return list;
    }

    @Override
    public int selectJoinCompanyNumByUserId(long companyUserId) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("selectJoinCompanyNumByUserId:companyUserId="+companyUserId);
        }
        int result=0;
        try{
            result=userCompanyRefMapper.selectJoinCompanyNumByUserId(companyUserId);
            result+=companyUserAuditMapper.selectCompanyNumByUserId(companyUserId);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackFrames(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public int selectCompanyUsers(long companyId) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("selectCompanyUsers:companyId="+companyId);
        }
        int result=0;
        try{
            result=userCompanyRefMapper.selectCompanyUsers(companyId);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackFrames(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public List<UserCompanyRef> userStatics(String begintime, String endtime, String companyName) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("userStatics : | begintime="+begintime+"|endtime="+endtime+"|companyName="+companyName);
        }
        List<UserCompanyRef> list=null;
        try{
            list=userCompanyRefMapper.userStatics(begintime, endtime, companyName);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return list;
    }

    @Override
    public PaginationList<UserCompanyRef> selectCompanyUserList(Long companyId, int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<UserCompanyRef> paginationList = new PaginationList<UserCompanyRef>();
        
        List<UserCompanyRef> list = null;
        int beginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = userCompanyRefMapper.selectCompanyUserList(companyId, beginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            logger.debug("selectUserCompanyRefByDytg:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = userCompanyRefMapper.selectCompanyUsers(companyId);
            }
            catch (DataAccessException e)
            {
                logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
    
    @Override
    public int selectInCompany(long userId,long companyId) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("selectJoinCompanyNumByUserId:userId="+userId+"|companyId="+companyId);
        }
        int result=0;
        try{
            result=userCompanyRefMapper.selectInCompany(userId, companyId);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackFrames(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
}
